const chalk = require('chalk');
const checkForUpdate = require('update-check');

const updateCheck = async (pkg) => {
  try {
    const update = await checkForUpdate(pkg);
    if (update) {
      process.on('exit', () => {
        console.log(chalk.cyan('\n╔══════════════════════════════════════════════════════════╗'));
        console.log(
          chalk.cyan('║            ') +
            `Update available ${chalk.red(pkg.version)} \u2192  ${chalk.greenBright(update.latest)}` +
            chalk.cyan('               ║')
        );
        console.log(
          chalk.cyan('║            ') +
            `Run ${chalk.blueBright('npm i -g ' + pkg.name)} to update` +
            chalk.cyan('                   ║')
        );
        console.log(chalk.cyan('╚══════════════════════════════════════════════════════════╝\n'));
      });
    }
    return update;
  } catch (err) {
    return err;
  }
};

module.exports = updateCheck;
